import { createStore } from 'vuex'
// import axios from 'axios'

import {
  getNowData,
  get7DaysData,
  getFutureHoursData,
  getCitylist,
  autoGetIP
} from '@/service/request'
// import { get3DaysData } from '@/service/request'

const store = createStore({
  state: () => {
    return {
      city: '',
      cityid: null,
      // 区域名字
      location: {
        adm1: '',
        adm2: ''
      },
      // 实时数据
      now: {
        temp: '10',
        text: '无数据',
        icon: '101',
        updateTime: null
      },
      // 未来24小时
      hourly: [],
      // 未来7天
      forecast: {
        days: [],
        weekdays: ['今天', '明天']
      },
      loadingTip: false
    }
  },

  mutations: {
    // updateNowWeather(state, payload) {
    //   state.now.temp = payload.data.now.temp
    //   state.now.text = payload.data.now.text
    // },
    // update3DaysWeather(state, payload) {
    //   state.now.temp = payload.data.now.temp
    //   state.now.text = payload.data.now.text
    // },
    updateCity(state, payload) {
      state.city = payload.res[0].data.location[0].name
      state.cityid = payload.res[0].data.location[0].id
      state.location.adm1 = payload.res[0].data.location[0].adm1
      state.location.adm2 = payload.res[0].data.location[0].adm2
    },
    updateWeather(state, payload) {
      // console.log(payload)
      // console.log(payload.res[0].data.updatetime

      // 实时天气
      state.now.temp = payload.res[0].data.now.temp
      state.now.text = payload.res[0].data.now.text
      state.now.icon = payload.res[0].data.now.icon
      state.now.updateTime = payload.res[0].data.updateTime

      // 未来3天
      state.forecast.days = payload.res[1].data.daily
      state.forecast.weekdays.splice(2, 5)
      const weekDay = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
      for (let i = 2; i < state.forecast.days.length; i++) {
        state.forecast.weekdays.push(
          weekDay[new Date(state.forecast.days[i].fxDate).getDay()]
        )
      }

      // 未来24小时
      const hourlyArr = payload.res[2].data.hourly
      state.hourly = hourlyArr.filter((item, index) => index % 2 === 0) //存下每两小时的数据
    },
    updateIP(state, payload) {
      state.city = payload.res.data.city
    }
  },

  actions: {
    getWeather(context, pos) {
      return Promise.all([
        getNowData(pos),
        get7DaysData(pos),
        getFutureHoursData(pos)
      ]).then(
        (res) => {
          console.log(res)
          context.commit('updateWeather', { res })
        },
        (err) => {
          console.log(err)
        }
      )
    },
    getCityInfo(context, pos) {
      return Promise.all([getCitylist(pos)]).then(
        (res) => {
          // console.log(res)
          context.commit('updateCity', { res })
        },
        (err1) => {
          console.log(err1)
        }
      )
    },
    getAutoIP(context, pos) {
      return new Promise((resolve, reject) => {
        resolve(autoGetIP(pos))
        reject('定位失败')
      }).then(
        (res) => {
          // console.log(res)
          const ar = res.data.rectangle
          const ip = ar.split(';')[0]
          context.dispatch('getWeather', ip)
          context.dispatch('getCityInfo', res.data.city)
          context.commit('updateIP', { res })
        },
        (err) => {
          console.log(err)
        }
      )
    }
  }
})

export default store
